//
//  UIView+Extension.m
//  ChatForHot
//
//  Created by wr on 2018/10/17.
//  Copyright © 2018年 FLY. All rights reserved.
//

CG_INLINE void
ReplaceMethod(Class _class, SEL _originSelector, SEL _newSelector) {
    Method oriMethod = class_getInstanceMethod(_class, _originSelector);
    Method newMethod = class_getInstanceMethod(_class, _newSelector);
    BOOL isAddedMethod = class_addMethod(_class, _originSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod));
    if (isAddedMethod) {
        class_replaceMethod(_class, _newSelector, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
    } else {
        method_exchangeImplementations(oriMethod, newMethod);
    }
}

#import "UIView+Extension.h"

@implementation UIView (Extension)

+ (id)viewFromNib {
    UIView * view = [[[NSBundle mainBundle] loadNibNamed:[[self class] description] owner:self options:nil] objectAtIndex:0];
    return view ? : nil;
}
@end

static char *EmptyViewKey = "EmptyViewKey";

@implementation UIView (Empty)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        ReplaceMethod([self class], @selector(layoutSubviews), @selector(ido_view_layoutSubviews));
    });
}

- (void)ido_view_layoutSubviews {
    [self layoutEmptyView];
    [self ido_view_layoutSubviews];
}

#pragma mark getters

- (HOTCHATEmptyView *)emptyView {
    return objc_getAssociatedObject(self, &EmptyViewKey);
}

- (BOOL)isEmptyViewShowing {
    return self.emptyView && self.emptyView.superview;
}

- (void)setEmptyView:(HOTCHATEmptyView *)emptyView {
    objc_setAssociatedObject(self, &EmptyViewKey, emptyView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

#pragma mark public

- (void)showEmptyView {
    if (!self.emptyView) {
        if ([self isKindOfClass:[UITableView class]] || [self isKindOfClass:[UICollectionView class]] || [self isKindOfClass:[UIScrollView class]]) {
            self.emptyView = [[HOTCHATEmptyView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height-((UIScrollView *)self).contentInset.top)];
        }else{
            self.emptyView = [[HOTCHATEmptyView alloc] initWithFrame:self.bounds];
        }
    }
    self.emptyView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.emptyView];
}

- (void)hideEmptyView {
    UIView *superView = [self.emptyView superview];
    if (superView != nil) {
        if ([superView isKindOfClass:[UITableView class]] || [superView isKindOfClass:[UICollectionView class]] || [superView isKindOfClass:[UIScrollView class]]) {
            ((UIScrollView *)superView).scrollEnabled = YES;
        }
    }
    [self.emptyView removeFromSuperview];
}

- (void)showEmptyViewWithLoading {
    [self showEmptyView];
    [self.emptyView setLoadingViewHidden:NO];
    [self.emptyView setImage:nil];
    [self.emptyView setTextLabelText:nil];
    [self.emptyView setDetailTextLabelText:nil];
    [self.emptyView setActionButtonTitle:nil];
}

- (void)showEmptyCommentViewWithReloadData:(EmptyActionButtonBlock)block {
    [self showEmptyViewWithImage:[UIImage imageNamed:@""]
                            text:nil
                      detailText:nil
                     buttonTitle:nil
                           block:block];
}

- (void)showErrorEmptyViewWithReloadData:(EmptyActionButtonBlock)block {
    [self showEmptyViewWithImage:[UIImage imageNamed:@""]
                            text:nil
                      detailText:nil
                     buttonTitle:nil
                           block:block];
}

- (void)showEmptyViewWithReloadData:(EmptyActionButtonBlock)block {
    [self showEmptyViewWithImage:[UIImage imageNamed:@""]
                            text:nil
                      detailText:nil
                     buttonTitle:nil
                           block:block];
}

- (void)showEmptyViewWithText:(NSString *)text
                   detailText:(NSString *)detailText
                  buttonTitle:(NSString *)buttonTitle
                 buttonAction:(SEL)action {
    [self showEmptyViewWithImage:nil text:text detailText:detailText buttonTitle:buttonTitle buttonAction:action];
}

- (void)showEmptyViewWithImage:(UIImage *)image
                          text:(NSString *)text
                    detailText:(NSString *)detailText
                   buttonTitle:(NSString *)buttonTitle
                  buttonAction:(SEL)action {
    [self showEmptyView];
    [self.emptyView setLoadingViewHidden:YES];
    [self.emptyView setImage:image];
    [self.emptyView setTextLabelText:text];
    [self.emptyView setDetailTextLabelText:detailText];
    [self.emptyView setActionButtonTitle:buttonTitle];
    [self.emptyView.actionButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
    [self.emptyView.actionButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
}

- (void)showEmptyViewWithText:(NSString *)text
                   detailText:(NSString *)detailText
                  buttonTitle:(NSString *)buttonTitle
                        block:(EmptyActionButtonBlock)block {
    [self showEmptyViewWithImage:nil text:text detailText:detailText buttonTitle:buttonTitle block:block];
}

- (void)showEmptyViewWithImage:(UIImage *)image
                          text:(NSString *)text
                    detailText:(NSString *)detailText
                   buttonTitle:(NSString *)buttonTitle
                         block:(EmptyActionButtonBlock)block {
    [self showEmptyView];
    [self.emptyView setLoadingViewHidden:YES];
    [self.emptyView setImage:image];
    [self.emptyView setTextLabelText:text];
    [self.emptyView setDetailTextLabelText:detailText];
    [self.emptyView setActionButtonTitle:buttonTitle];
    [self.emptyView setEmptyTapBlock:block];
}

- (void)showEmptyViewWithImage:(UIImage *)image {
    [self showEmptyView];
    [self.emptyView setLoadingViewHidden:YES];
    [self.emptyView setImage:image];
    [self.emptyView setTextLabelText:nil];
    [self.emptyView setDetailTextLabelText:nil];
    [self.emptyView setActionButtonTitle:nil];
}

- (BOOL)layoutEmptyView {
    if (self.emptyView) {
        BOOL viewDidLoad = self.emptyView.superview;
        if (viewDidLoad) {
            CGSize newEmptyViewSize = self.emptyView.superview.bounds.size;
            CGSize oldEmptyViewSize = self.emptyView.frame.size;
            if (!CGSizeEqualToSize(newEmptyViewSize, oldEmptyViewSize)) {
                self.emptyView.frame = CGRectMake(CGRectGetMinX(self.emptyView.frame), CGRectGetMinY(self.emptyView.frame), newEmptyViewSize.width, newEmptyViewSize.height);
            }
            return YES;
        }
    }
    
    return NO;
}
@end
